﻿package com.bizmlib.controls
{
	import fl.core.UIComponent;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import fl.events.ColorPickerEvent;
	import flash.geom.Point;
	import flash.geom.ColorTransform;
	import com.bizmlib.ui.composite.*;
	
	
	public class ColorPicker extends UIComponent
	{
		private var border:MovieClip;
		private var colorWell:MovieClip;
		private var noColorIcon:MovieClip;
		private var swatches:ColorPickerSwatchesPanel;
		
		private var __colors:Array;
		private var __color:Number;
		private var colorsDataProvider:Array;
		
		
		public function ColorPicker()
		{
		}
		
		
		public function set colors(value:Array):void
		{
			__colors = value;
			updateColors();
			invalidate();
		}
		public function get colors():Array
		{
			return __colors;
		}
		public function set color(value:Number):void
		{
			//trace(this + ".color (set) " + arguments);
			__color = value;
			invalidate();
		}
		public function get color():Number
		{
			return __color;
		}
		public function get colorAlpha():Number
		{
			return (color < 0) ? 0 : 1;
		}
		
		
		override protected function configUI():void
		{
			super.configUI();
			
			colorWell = new ColorPicker_colorWell();
			addChild(colorWell);
			colorWell.addEventListener(MouseEvent.MOUSE_DOWN, openInvoked);
			
			noColorIcon = new ColorPicker_noColorIcon();
			addChild(noColorIcon);
			noColorIcon.mouseEnabled = false;
			noColorIcon.visible = false;
			
			border = new ColorPicker_borderSkin();
			addChild(border);
			border.mouseEnabled = false;
			border.mouseChildren = false;
			
			swatches = new ColorPickerSwatchesPanel();
			swatches.addEventListener(UICompositionEvent.ITEM_SELECT, swatchSelected);
			swatches.show();
			
			var currentID:Number = 0;
			var colors:Array = new Array();
			for(var i:Number = 0; i<12; i++)
			{
				for(var j:Number=0; j<18; j++)
				{
					colors.push((Math.floor(i/6)*3 + Math.floor(j/6))*0x330000 + (i%6)*0x33/**/ + (j%6)*0x3300/**/);
				}
			}
			colors.push(-1);
			this.colors = colors;
			
			/**
			swatches.dataProvider = [
				{id:0, color:0xff0000},
				{id:1, color:0x00ff00},
				{id:2, color:0x0000ff}
			];
			/**/
		}
		
		override protected function draw():void
		{
			//trace(this + ".draw(" + arguments + ")");
			//trace(color);
			
			colorWell.width = width;
			colorWell.height = height;
			var colorTransform:ColorTransform = new ColorTransform();
			colorTransform.color = color;
			colorWell.transform.colorTransform = colorTransform;
			
			border.width = width;
			border.height = height;
			
			noColorIcon.width = width;
			noColorIcon.height = height;
			noColorIcon.visible = color < 0;
			
			super.draw();
		}
		
		
		
		private function openInvoked(event:MouseEvent = null):void
		{
			//trace(this + ".openInvoked(" + arguments + ")");
			event.stopImmediatePropagation();
			open();
		}
		private function closeInvoked(event:MouseEvent = null):void
		{
			//trace(this + ".closeInvoked(" + arguments + ")");
			close();
		}
		
		private function open():void
		{
			colorWell.removeEventListener(MouseEvent.MOUSE_DOWN, openInvoked);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, closeInvoked);
			
			stage.addChild(swatches);
			positionSwatchesPanel();
		}
		private function close():void
		{
			colorWell.addEventListener(MouseEvent.MOUSE_DOWN, openInvoked);
			stage.removeEventListener(MouseEvent.MOUSE_DOWN, closeInvoked);
			
			stage.removeChild(swatches);
		}
		
		private function updateColors():void
		{
			colorsDataProvider = new Array();
			for(var i:Number=0; i<colors.length; i++)
			{
				colorsDataProvider[i] = {id:i, color:colors[i]};
			}
			swatches.dataProvider = colorsDataProvider;
		}
		
		private function positionSwatchesPanel():void
		{
			var point:Point = localToGlobal(new Point(0, 0));
			stage.addChild(swatches);
			swatches.x = point.x;
			swatches.y = point.y + height + 1;
		}
		
		private function swatchSelected(event:UICompositionEvent):void
		{
			//trace(this + ".swatchSelected(" + arguments + ")");
			color = event.dataProvider.color;
			
			//trace("color=" + color);
			dispatchEvent(new ColorPickerEvent(ColorPickerEvent.CHANGE, color));
		}
	}
}